package com.coolgor.axnut.schedule.lordsee;

import com.coolgor.axnut.entity.lordsee.ScanIPResult;
import com.coolgor.axnut.service.lordsee.ScanIPResultService;
import com.coolgor.axnut.util.NetworkUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;


public class ScanShutdownRunnable implements Runnable{

    @Autowired
    ScanIPResultService scanIPResultService;

    private List<String> list;
    private Integer p = 0 ;
    private List<String> result = new ArrayList<String>();

    public void setList(List list)
    {
        this.list = list;
    }

    public List<String> getResult(){
        return this.result;
    }

    @Override
    public void run() {
        System.out.println("执行了一个线程:"+Thread.currentThread().getName());
        ScanIPResult scanIPResult;
        String ip = null;
        if ( null != list ) {
            while (p < list.size()) {
                synchronized (p) {
                    if (p < list.size()) ip = list.get(p);
                    p++;
                }
                if (null != ip) {
                    if (NetworkUtil.inetPing(ip)) {
                        System.out.println(Thread.currentThread().getName()+" : "+ip+" : OK");
                        synchronized(result){
                            result.add(ip);
                        }
//                        scanIPResult = new ScanIPResult();
//                        synchronized (scanIPResult) {
//                            scanIPResult.setIp(ip);
//                            scanIPResult.setStatus("ping");
//                            scanIPResult.setScanTime(new Date());
//                            scanIPResultService.addScanIPResult(scanIPResult);
//                        }
                    }
                }
                //            try {
                //                Thread.sleep(1);
                ////                wait(1);
                //            } catch (InterruptedException e) {
                //                e.printStackTrace();
                //            }
            }
        }
    }

}